class AccountPwdController{
    constructor($state,$rootScope,$scope,UserService,AccountService){
        Object.assign(this,{$state,$rootScope,$scope,UserService,AccountService});
        this.$rootScope.currentGroup= this.$state.params.cg;
        this.curr_pwd = "";
        this.new_pwd = "";
        this.confirm_new_pwd = "";
        this.init();
    }

    init(){
        let self = this;
        self.UserService.getQuestionList({}).then(_success).catch(_error);
        function _success(data) {
            self.questionList =   data.data;
        }
        function _error(reason) {
            console.log(reason);
        }
        self.AccountService.getAccountInfo({ignoreLoadingBar:true}).then(account_success).catch(account_error);
        function account_success(data) {
            self.lotteryAccountInfo =data;
        };
        function account_error(reason) {
            console.log("AccountPwdController.init(): "+ JSON.stringify(reason));
        };
    }

    /**
     * 修改密码
     */
    modifyPwd(){
        let self = this;
        if(self.$scope.pwd.new_pwd != self.$scope.pwd.confirm_new_pwd ){
            layer.alert("对不起，新密码与确认新密码不相等！",{
                title:"错误",
                icon: 2,
               btn:["知道了"],
               btn1:function (index,layerro) {
                   layer.close(index);
               }
            });
            return ;
        }
        layer.confirm("确认要修改密码吗？",{
            btn:["确定","不修改了"],
            btn1:function (index,layerro) {
                //执行修改
                layer.close(index);
                let param = {"curr_pwd":self.curr_pwd,"new_pwd":self.new_pwd,"confirm_new_pwd":self.confirm_new_pwd};
                if(self.question &&　self.answer){
                    param.question = self.question.question_context;
                    param.answer = self.answer;
                }
                self.UserService.updatePwd(param).then(_success).catch(_error);
            },
            btn2:function (index,layerro) {
                layer.close(index);
            }
        });
        function _success(data) {
            if(data.success==true){
                layer.alert("成功修改密码，请您重新登录。",{
                    title:"信息",
                    icon: 1,
                    btn:["好的"],
                    btn1:function (index,layerro) {
                        layer.close(index);
                        self.$state.go("login");
                    }
                });
            }
        }
        function _error(reason) {
            layer.alert(reason&&reason.text?reason.text:"对不起操作失败！",{
                title:"错误",
                icon: 2,
                btn:["知道了"],
                btn1:function (index,layerro) {
                    layer.close(index);
                }
            });
        }
    }
}
AccountPwdController.$inject = ["$state","$rootScope","$scope","UserService","AccountService"];

export default AccountPwdController;